#include <algorithm>
#include <cstdint>
#include <cstring>
#include <ios>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using ll = int64_t;

struct Edge{
    ll to{};
    ll w{};
    inline bool operator<(const Edge&r){
        return w<r.w;
    }
};

struct XYZ{
    ll x{},y{},z{};
    inline bool operator<(const XYZ&r)const{
        return z<r.z;
    }
};

const ll maxlog = 15, inf=1e9;
ll n,m,q;
std::vector<std::vector<Edge>> edg;
std::vector<ll> unf, dpth;
std::vector<std::vector<ll>> fth,cost;
std::priority_queue<XYZ> pq;
ll mergednum = 0;

static inline ll find(ll idx){
    if(idx==unf[idx])return idx;
    return unf[idx]=find(unf[idx]);
}

static inline void merge(ll a,ll b){
    unf[find(a)]=find(b);
}

static inline bool ismerged(ll a,ll b){
    return find(a)==find(b);
}

static inline void dfs(ll f,ll cur){
    dpth[cur]=dpth[f]+1;
    fth[cur][0]=f;
    for(ll i=1;i<=maxlog;i++){
        fth[cur][i]=fth[fth[cur][i-1]][i-1];
        cost[cur][i]=std::min(cost[fth[cur][i-1]][i-1],cost[cur][i-1]);
    }
    for(auto nxt:edg[cur]){
        if(nxt.to==f)continue;
        cost[nxt.to][0]=nxt.w;
        dfs(cur,nxt.to);
    }
}

static inline ll lca(ll a,ll b){
    if(dpth[a]>dpth[b])std::swap(a,b);
    // ll tmp = dpth[b]-dpth[1],ans=0;错误
    ll tmp = dpth[b]-dpth[a],ans=inf;
    for(ll j=0;tmp;j++,tmp>>=1){
        if(tmp&1){
            ans=std::min(ans,cost[b][j]);
            b=fth[b][j]; // 注意在if里面
        }
    }
    if(a==b)return ans;
    for(ll j=maxlog;j>=0 && a!=b;j--){
        if(fth[a][j]!=fth[b][j]){
            ans=std::min({ans,cost[a][j],cost[b][j]});
            a=fth[a][j];
            b=fth[b][j];
        }
    }
    ans=std::min({ans,cost[a][0],cost[b][0]});
    return ans;
}   

int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n>>m;

    edg.resize(n+1);
    unf.resize(n+1);
    fth.resize(n+1,std::vector<ll>(maxlog+1));
    cost.resize(n+1,std::vector<ll>(maxlog+1,inf));
    dpth.resize(n+1);

    for(ll i=1;i<=n;i++){
        unf[i]=i;
    }

    for(ll i=1;i<=m;i++){
        ll x,y,z;
        std::cin>>x>>y>>z;
        pq.emplace(x,y,z);
    }

    while(pq.size() && mergednum < n-1){
        auto[x,y,z] = pq.top();
        pq.pop();
        if(ismerged(x, y))continue;
        edg[x].emplace_back(y,z);
        edg[y].emplace_back(x,z);
        merge(x, y);
        mergednum++;
    }

    // for(ll u=1;u<=n;u++){
    //     for(auto [v,w]:edg[u]){
    //         printf("point %lld to %lld weight %lld\n",u,v,w);
    //     }
    // }

    for(ll i=1;i<=n;i++){
        if(dpth[i]==0){
            dpth[0]=0;
            dfs(0,i);
        }
    }
    std::cin>>q;
    for(ll i=1;i<=q;i++){
        ll a,b;
        std::cin>>a>>b;
        if (!ismerged(a, b)) {
            std::cout<<"-1\n";
            continue;
        }
        std::cout<<lca(a,b)<<'\n';
    }
}